home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Mud 4.0 / resolver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  4.5 KB  |  207 lines  |  [TEXT/MPS ]

  1.  
  2. #include <Resources.h>
  3. #include <Files.h>
  4. #include <OSUtils.h>
  5. #include <Memory.h>
  6. #include <Errors.h>
  7. #include <Traps.h>
  8. #include <GestaltEqu.h>
  9. #include <Folders.h>
  10. #include <ToolUtils.h>
  11.  
  12. #define OPENRESOLVER    1
  13. #define CLOSERESOLVER    2
  14. #define STRTOADDR        3
  15. #define    ADDRTOSTR        4
  16. #define    ENUMCACHE        5
  17. #define ADDRTONAME        6
  18. #define    HINFO            7
  19. #define MXINFO            8
  20.  
  21. Handle codeHndl = nil;
  22.  
  23. typedef OSErr (*OSErrProcPtr)();
  24. OSErrProcPtr dnr = nil;
  25.  
  26.  
  27. TrapType GetTrapType(theTrap)
  28. unsigned long theTrap;
  29. {
  30.     if (BitAnd(theTrap, 0x0800) > 0)
  31.         return(ToolTrap);
  32.     else
  33.         return(OSTrap);
  34. }
  35.  
  36. Boolean TrapAvailable1(trap)
  37. unsigned long trap;
  38. {
  39.     TrapType trapType = ToolTrap;
  40.     unsigned long numToolBoxTraps;
  41.  
  42.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  43.         numToolBoxTraps = 0x200;
  44.     else
  45.         numToolBoxTraps = 0x400;
  46.  
  47.     trapType = GetTrapType(trap);
  48.     if (trapType = ToolTrap) {
  49.         trap = BitAnd(trap, 0x07FF);
  50.         if (trap > numToolBoxTraps)
  51.             trap = _Unimplemented;
  52.     }
  53.     return(NGetTrapAddress(trap, trapType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  54. }
  55.  
  56. short GetCPanelFolder()
  57. {
  58.     short vRefNum = 0;
  59.     long dirID = 0;
  60.     SysEnvRec info;
  61.     Boolean hasFolderMgr, hasGestalt = false;
  62.     long feature;
  63.     short wdRef;
  64.  
  65.     dirID = 0;
  66.     if (TrapAvailable1(_GestaltDispatch)) if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
  67.     if (!hasFolderMgr) {
  68.         SysEnvirons(1, &info);
  69.         return(info.sysVRefNum);
  70.     }
  71.     else {
  72.         if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, &vRefNum, &dirID) != noErr) return(0);
  73.         if (OpenWD(vRefNum, dirID, 'dnrp', &wdRef) == noErr)
  74.             return(wdRef);
  75.         else
  76.             return(0);
  77.     }
  78. }
  79.  
  80. /* OpenOurRF is called to open the MacTCP driver resources */
  81. short OpenOurRF()
  82. {
  83.     ParamBlockRec fi;
  84.     Str255 filename;
  85.     short vRefNum;
  86.  
  87.     vRefNum = GetCPanelFolder();
  88.     fi.fileParam.ioCompletion = nil;
  89.     fi.fileParam.ioNamePtr = &filename;
  90.     fi.fileParam.ioVRefNum = vRefNum;
  91.     fi.fileParam.ioFDirIndex = 1;
  92.  
  93.     while (PBGetFInfo(&fi, false) == noErr) {
  94.         /* scan system folder for driver resource files of specific type & creator */
  95.         if (fi.fileParam.ioFlFndrInfo.fdType == 'cdev' &&
  96.             fi.fileParam.ioFlFndrInfo.fdCreator == 'ztcp') {
  97.             /* found the MacTCP driver file */
  98.             return(OpenRFPerm(&filename, vRefNum, fsRdPerm));
  99.         }
  100.         /* check next file in system folder */
  101.         fi.fileParam.ioFDirIndex++;
  102.     }
  103.     return(-1);
  104. }    
  105.  
  106. OSErr OpenResolver(fileName)
  107. char *fileName;
  108. {
  109.     short refnum;
  110.     OSErr rc;
  111.     long feature = 0;
  112.     Boolean hasTimeMgr = false;
  113.  
  114.     if (dnr != nil)
  115.         /* resolver already loaded in */
  116.         return(noErr);
  117.  
  118.     /* open the MacTCP driver to get DNR resources. Search for it based on
  119.        creator & type rather than simply file name */    
  120.     refnum = OpenOurRF();
  121.  
  122.     /* ignore failures since the resource may have been installed in the 
  123.        System file if running on a Mac 512Ke */
  124.  
  125.     /* load in the DNR resource package */
  126.     codeHndl = GetIndResource('dnrp', 1);
  127.  
  128.     if (codeHndl == nil) {
  129.         /* can't open DNR */
  130.         return(ResError());
  131.     }
  132.  
  133.     DetachResource(codeHndl);
  134.     if (refnum != -1) {
  135.         CloseWD(refnum);
  136.         CloseResFile(refnum);
  137.     }
  138.  
  139.     /* lock the DNR resource since it cannot be reloated while opened */
  140.     HLock(codeHndl);
  141.     dnr = (OSErrProcPtr) *codeHndl;
  142.  
  143.     /* call open resolver */
  144.     rc = (*dnr)(OPENRESOLVER, fileName);
  145.     if (rc != noErr) {
  146.         /* problem with open resolver, flush it */
  147.         HUnlock(codeHndl);
  148.         DisposHandle(codeHndl);
  149.         dnr = nil;
  150.     }
  151.  
  152.     /* check is the extended time manager is available */
  153.     if (TrapAvailable1(_GestaltDispatch)) if (Gestalt(gestaltTimeMgrVersion, &feature) == noErr) hasTimeMgr = true;
  154.     if (hasTimeMgr) {
  155.         if ((feature == gestaltExtendedTimeMgr) || (feature == gestaltRevisedTimeMgr))
  156.             rc == noErr;
  157.         else rc == 1;
  158.     }
  159.  
  160.     return(rc);
  161. }
  162.  
  163. OSErr CloseResolver()
  164. {
  165.     if (dnr == nil)
  166.         /* resolver not loaded error */
  167.         return(notOpenErr);
  168.  
  169.     /* call close resolver */
  170.     (void) (*dnr)(CLOSERESOLVER);
  171.  
  172.     /* release the DNR resource package */
  173.     HUnlock(codeHndl);
  174.     DisposHandle(codeHndl);
  175.     dnr = nil;
  176.     return(noErr);
  177. }
  178.  
  179. OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
  180. char *hostName;
  181. struct hostInfo *rtnStruct;
  182. long resultproc;
  183. char *userDataPtr;
  184. {
  185.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  186. }
  187.  
  188. void AddrToStr(addr, addrStr)
  189. unsigned long addr;
  190. char *addrStr;                                    
  191. {
  192.     (*dnr)(ADDRTOSTR, addr, addrStr);
  193. }
  194.  
  195. OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
  196. unsigned long addr;
  197. struct hostInfo *rtnStruct;
  198. long resultproc;
  199. char *userDataPtr;                                    
  200. {
  201.     if (dnr == nil)
  202.         /* resolver not loaded error */
  203.         return(notOpenErr);
  204.  
  205.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  206. }
  207.